import 'package:flutter/material.dart';
import 'pages/login.dart';
import 'pages/welcome.dart';
import 'pages/musicList.dart';
import 'util/DataUtils.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            title: '奇妙时代',
            home: new InitWidget(),
        );
    }
}

class InitWidget extends StatelessWidget {
    
    InitWidget() {
        print('InitWidget--构造函数');
    }

    @override
    Widget build(BuildContext context) {
        DataUtils.isLogin().then((res) {
            if (res) {
                // 已登录
                print('InitWidget--已登录，跳转到欢迎页');
                //Navigator.of(context).push(new MaterialPageRoute(
                //    builder: (ctx) => new Welcome()
                //));

                Navigator.of(context).pushAndRemoveUntil(
                    new MaterialPageRoute(builder: (context) => new MusicList()
                    ), (route) => route == null);
            } else {
                // 未登录
                print('InitWidget--未登录，跳转到登录页');
                //Navigator.of(context).push(new MaterialPageRoute(
                //    builder: (ctx) => new Login()
                //));

                Navigator.of(context).pushAndRemoveUntil(
                    new MaterialPageRoute(builder: (context) => new LoginPage()
                    ), (route) => route == null);
            }
        });
        //*/

        return new Scaffold(
            body: new Center(
                child: new Text('加载中...'),
            ),
        );
    }
}

// 测试按钮跳转到指定的页面
class MainWidget extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
        return new Scaffold(
            body: new Container(
                margin: const EdgeInsets.fromLTRB(0.0, 100.0, 0.0, 0.0),
                child: Flex(
                    direction: Axis.horizontal,
                    children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: Container(
                                height: 30.0,
                                //color: Colors.red,
                                child: new Center(
                                    child: new FlatButton(
                                        child: Text("Login"),
                                        onPressed: () {
                                            print("Login ... ");
                                            Navigator.of(context).push(new MaterialPageRoute(
                                                builder: (ctx) => new LoginPage()
                                            ));
                                        },
                                    )
                                ),
                            ),
                        ),
                        Expanded(
                            flex: 1,
                            child: Container(
                                height: 30.0,
                                //color: Colors.green,
                                child: new Center(
                                    child: new FlatButton(
                                        child: Text("Welcome"),
                                        onPressed: () {
                                            print("Welcome ... ");
                                            Navigator.of(context).push(new MaterialPageRoute(
                                                builder: (ctx) => new Welcome()
                                            ));
                                        },
                                    )
                                ),
                            ),
                        ),
                    ],
                )
            ),
        );
    }
}